package com.slicktechnologies.client.views.settings.branch;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.TextBox;
import com.simplesoftwares.client.library.FieldType;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appskeleton.ScreeenState;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.simplesoftwares.client.library.appstructure.formtablescreen.FormTableScreen;
import com.simplesoftwares.client.library.composite.AddressComposite;
import com.simplesoftwares.client.library.mywidgets.EmailTextBox;
import com.simplesoftwares.client.library.mywidgets.PhoneNumberBox;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
/**
 * FormTablescreen template.
 */
public class BranchForm extends FormTableScreen<Branch>  implements ClickHandler {

	//Token to add the varialble declarations
	EmailTextBox etbEmail,etbPointOfContactEmail;
	AddressComposite addressComposite;
	CheckBox checkBoxStatus;
	TextBox tbBranchName,tbPointOfContactName;
	//ObjectListBox<LeaveCalendar>calendar;
	PhoneNumberBox pnbLandlinePhone,pnbCellPhoneNo1,pnbCellPhoneNo2,pnbFaxNo,pnbPointOfContactLandline,pnbPointOfContactCell;


	//Token to add the concrete presenter class name
	//protected <PresenterClassName> presenter;



	public BranchForm  (SuperTable<Branch> table, int mode,
			boolean captionmode) {
		super(table, mode, captionmode);
		createGui();

	}

	private void initalizeWidget()
	{

		etbEmail=new EmailTextBox();

		etbPointOfContactEmail=new EmailTextBox();

		addressComposite=new AddressComposite();

		checkBoxStatus=new CheckBox();
		checkBoxStatus.setValue(true); 
		tbBranchName=new TextBox();

		tbPointOfContactName=new TextBox();

		pnbLandlinePhone=new PhoneNumberBox();

		pnbCellPhoneNo1=new PhoneNumberBox();

		pnbCellPhoneNo2=new PhoneNumberBox();

		pnbFaxNo=new PhoneNumberBox();

		pnbPointOfContactLandline=new PhoneNumberBox();

		pnbPointOfContactCell=new PhoneNumberBox();
		/*calendar=new ObjectListBox<LeaveCalendar>();
		LeaveCalendar.makeObjectListBoxLive(calendar);*/

	}

	/*
	 * Method template to create the formtable screen
	 */

	@Override
	public void createScreen() {

		//Token to initialize the processlevel menus.

	    initalizeWidget();
		
		this.processlevelBarNames=new String[]{"New"};
		

		//////////////////////////////////Form Field Declaration/////////////////////////////////////////////////


		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////

		//Token to initialize formfield
		FormFieldBuilder fbuilder;
		fbuilder = new FormFieldBuilder();
		FormField fgroupingCompanyInformation=fbuilder.setlabel("Branch Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("* Branch Name",tbBranchName);
		FormField ftbBranchName= fbuilder.setMandatory(true).setMandatoryMsg("Branch Name is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Branch Email",etbEmail);
		FormField fetbEmail= fbuilder.setMandatory(true).setMandatoryMsg("Email is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Status",checkBoxStatus);
		FormField fcheckBoxStatus= fbuilder.setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Landline Phone",pnbLandlinePhone);
		FormField fpnbLandlinePhone= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Cell Phone No 1",pnbCellPhoneNo1);
		FormField fpnbCellPhoneNo1= fbuilder.setMandatory(true).setMandatoryMsg("Cell Number 1 is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Cell Phone No 2",pnbCellPhoneNo2);
		FormField fpnbCellPhoneNo2= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Fax No",pnbFaxNo);
		FormField fpnbFaxNo= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingPointOfContactInformation=fbuilder.setlabel("Point Of Contact Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("* Point Of Contact Name",tbPointOfContactName);
		FormField ftbPointOfContactName= fbuilder.setMandatory(true).setMandatoryMsg("Point Of Contact Name is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Point Of Contact Landline",pnbPointOfContactLandline);
		FormField fpnbPointOfContactLandline= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Point Of Contact Cell",pnbPointOfContactCell);
		FormField fpnbPointOfContactCell= fbuilder.setMandatory(true).setMandatoryMsg("Point Of Contact Cell is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Point Of Contact Email",etbPointOfContactEmail);
		FormField fetbPointOfContactEmail= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingAddressInformation=fbuilder.setlabel("Address Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("",addressComposite);
		FormField faddressComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
       /*
		fbuilder = new FormFieldBuilder("Calendar",calendar);
		FormField fcalendar= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
        */


		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////


		FormField[][] formfield = {   {fgroupingCompanyInformation},

  {ftbBranchName,fetbEmail,fcheckBoxStatus},
  {fpnbLandlinePhone,fpnbCellPhoneNo1,fpnbCellPhoneNo2,fpnbFaxNo},
  {fgroupingPointOfContactInformation},
  {ftbPointOfContactName,fpnbPointOfContactLandline,fpnbPointOfContactCell,fetbPointOfContactEmail},
  {fgroupingAddressInformation},
  {faddressComposite},
  };



		this.fields=formfield;		
	}

	/**
	 * method template to update the model with token entity name
	 */
	@Override
	public void updateModel(Branch model) 
	{

		if(tbBranchName.getValue()!=null)
			model.setBusinessUnitName(tbBranchName.getValue());
		if(etbEmail.getValue()!=null)
			model.setEmail(etbEmail.getValue());
		if(checkBoxStatus.getValue()!=null)
			model.setstatus(checkBoxStatus.getValue());
		if(pnbLandlinePhone.getValue()!=null)
			model.setLandline(pnbLandlinePhone.getValue());
		if(pnbCellPhoneNo1.getValue()!=null)
			model.setCellNumber1(pnbCellPhoneNo1.getValue());
		if(pnbCellPhoneNo2.getValue()!=null)
			model.setCellNumber2(pnbCellPhoneNo2.getValue());
		if(pnbFaxNo.getValue()!=null)
			model.setFaxNumber(pnbFaxNo.getValue());
		if(tbPointOfContactName.getValue()!=null)
			model.setPocName(tbPointOfContactName.getValue());
		if(pnbPointOfContactLandline.getValue()!=null)
			model.setPocLandline(pnbPointOfContactLandline.getValue());
		if(pnbPointOfContactCell.getValue()!=null)
			model.setPocCell(pnbPointOfContactCell.getValue());
		if(etbPointOfContactEmail.getValue()!=null)
			model.setPocEmail(etbPointOfContactEmail.getValue());
		if(addressComposite.getValue()!=null)
			model.setAddress(addressComposite.getValue());

		/*model.setCalendar(calendar.getSelectedItem());
		if(calendar.getValue()!=null)
			model.setCalendarName(calendar.getValue());
		else
			model.setCalendarName("");*/

		presenter.setModel(model);


	}

	/**
	 * method template to update the view with token entity name
	 */
	@Override
	public void updateView(Branch view) 
	{


		if(view.getBusinessUnitName()!=null)
			tbBranchName.setValue(view.getBusinessUnitName());
		if(view.getEmail()!=null)
			etbEmail.setValue(view.getEmail());
		if(view.getstatus()!=null)
			checkBoxStatus.setValue(view.getstatus());
		if(view.getLandline()!=null)
			pnbLandlinePhone.setValue(view.getLandline());
		if(view.getCellNumber1()!=null)
			pnbCellPhoneNo1.setValue(view.getCellNumber1());
		if(view.getCellNumber2()!=null)
			pnbCellPhoneNo2.setValue(view.getCellNumber2());
		if(view.getFaxNumber()!=null)
			pnbFaxNo.setValue(view.getFaxNumber());
		if(view.getPocName()!=null)
			tbPointOfContactName.setValue(view.getPocName());
		if(view.getPocLandline()!=null)
			pnbPointOfContactLandline.setValue(view.getPocLandline());
		if(view.getPocCell()!=null)
			pnbPointOfContactCell.setValue(view.getPocCell());
		if(view.getPocEmail()!=null)
			etbPointOfContactEmail.setValue(view.getPocEmail());
		if(view.getAddress()!=null)
			addressComposite.setValue(view.getAddress());
		/*if(view.getCalendarName()!=null)
			calendar.setValue(view.getCalendarName());*/
		presenter.setModel(view);



	}

	/**
	 * Toggles the app header bar menus as per screen state
	 */

	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard")||text.contains("Sillenium Test"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   

			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Edit")||text.contains("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
			}
		}
		
	}

	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{

	}

	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear() {
		super.clear();
		checkBoxStatus.setValue(true);
	}



}
